package com.lgt_enterprise_android.view.pages;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.StringUtil;
import com.widgets.CustomizeTitleView;

public class WebViewActivity extends BaseActivity {

    private String url;
    private WebView mWebView;
    private ProgressBar progressBar1;

    private CustomizeTitleView customizeTitleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        initView();
    }

    private void initView(){
        mWebView = findViewById(R.id.webview);
//        mTitleView = findViewById(R.id.custom_title);
        progressBar1 = findViewById(R.id.progressBar1);
        customizeTitleView = (CustomizeTitleView) findViewById(R.id.customizeTitleView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setAppCacheEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);

        try {
            webSettings.setDomStorageEnabled(true);
            webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    return false;
                }
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    return true;//没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progressBar1.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    progressBar1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar1.setProgress(newProgress);//设置进度值
                }
            }
        });

        initData();
    }

    public void initData() {
        url = getIntent().getStringExtra("url");
        customizeTitleView.setTitle(getIntent().getStringExtra("title"));
        mWebView.loadUrl(url == null ? "" : url);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String title = view.getTitle();
                if (!StringUtil.isEmpty(title)) {
                    customizeTitleView.setTitle(title);
                }
            }
        });


    }


    @Override
    public void onBackPressed() {
        if (mWebView!=null && mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }
}
